<template>
  <div class="dashboard-container">
    <h1 style="text-align: center">房间管理</h1>


    <el-dialog
      title="新增"
      :visible.sync="addButtonController"
      width="50%"
      @close="closeRoom('ruleForm')"
    >
      <el-form label-width="20%" :model="addOrUpdateList" :rules="rooms" ref="ruleForm">
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="addOrUpdateList.roomNumber" maxlength="11" show-word-limit style="width: 80%" clearable
                    placeholder="请输入房间号"
          />
        </el-form-item>
        <el-form-item label="房间类型" prop="roomTypeName">
          <el-select v-model="addOrUpdateList.roomTypeId" clearable placeholder="房间类型" style="width: 80%">
            <el-option
              v-for="item in roomOptions"
              :key="item.roomTypeId"
              :label="item.roomTypeName + '-' + item.roomPrice"
              :value="item.roomTypeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addOrUpdateList.remark" maxlength="100" show-word-limit style="width: 80%" clearable
                    placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addRoom('ruleForm')">确 定</el-button>
        <el-button @click="closeRoom('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>
    <el-button plain
               style="width:80px;margin-bottom:30px;margin-left:18% ;background-color: #cfefc6; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
               @click="addButton"
    >
      新增
    </el-button>

    <el-dialog
      title="修改"
      :visible.sync="updateButtonController"
      width="50%"
      @close="closeRoom('ruleForm')"
    >
      <el-form label-width="20%" :model="addOrUpdateList" :rules="rooms" ref="ruleForm">
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="addOrUpdateList.roomNumber" maxlength="11" show-word-limit style="width: 80%" clearable
                    placeholder="请输入房间号"
          />
        </el-form-item>
        <!-- 房间类型选择表单项 -->
        <el-form-item label="房间类型" prop="roomTypeId">
          <!-- 使用 el-select 组件创建房间类型下拉选择框 -->
          <el-select v-model="addOrUpdateList.roomTypeId" clearable placeholder="房间类型" style="width: 80%">
            <!-- 遍历 roomOptions 数组生成下拉选项 -->
            <el-option
              v-for="item in roomOptions"
              :key="item.roomTypeId"
              :label="item.roomTypeName + '-' + item.roomPrice"
              :value="item.roomTypeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addOrUpdateList.remark" maxlength="100" show-word-limit style="width: 80%" clearable
                    placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateRoom('ruleForm')">确 定</el-button>
        <el-button @click="closeRoom('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :visible.sync="deleteButtonController"
      title="您确认要删除吗？"
      width="30%"
      @close="closeDelete('ruleForm')"
    >
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="deleteRoom('ruleForm')">确 定</el-button>
    <el-button @click="closeDelete('ruleForm')">取 消</el-button>
  </span>
    </el-dialog>


    <el-input
      v-model="roomNumber"
      placeholder="房间号"
      name="username"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />

    <el-select v-model="roomTypeName" clearable placeholder="房间类型" style="margin-left: 40px;width: 200px">
      <el-option
        v-for="item in roomOptions"
        :key="item.roomTypeId"
        :label="item.roomTypeName + '-' + item.roomPrice"
        :value="item.roomTypeName"
      >
      </el-option>
    </el-select>


    <el-button plain
               style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: #cfefc6; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
               @click="selectAll"
    >
      查询
    </el-button>
    <el-button
      style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
      @click="checkRoom"
    >重置
    </el-button>
    <el-table :data="roomList.slice((currentPage-1)*pagesize,currentPage*pagesize)" height="659px" class="elegant-room-table" border style="width: 100%;margin-top: 20px">
      <el-table-column align="center" fixed prop="roomId" label="房间ID" width="140"/>
      <el-table-column align="center" fixed prop="roomNumber" label="房间号" width="140"/>
      <el-table-column align="center" fixed prop="roomTypeName" label="房间类型" width="180"/>
      <el-table-column align="center" fixed prop="roomPrice" label="房间价格" width="150">
        <template v-slot="scope">
          <span class="price-tag">¥{{ scope.row.roomPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed prop="remark" label="备注" min-width="200" show-overflow-tooltip/>
      <el-table-column align="center" fixed label="操作" width="180">
        <template v-slot="scope">
          <el-button size="mini" round icon="el-icon-edit" @click="updateButton(scope.row)"></el-button>
          <el-button size="mini" round icon="el-icon-delete" @click="deleteButton(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:5px;margin-left: 30%;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10,15,20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="roomList.length">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteRoom, insertRoom, selectAllRoom, updateRoom } from '@/api/room'
import { deleteRoomType, selectAllRoomType } from '@/api/roomType'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data() {
    return {
      roomNumber: '',
      roomTypeName: '',
      roomTypeId: '',
      roomList: [],
      roomOptions: [], // 下拉
      addButtonController: false,
      updateButtonController: false,
      deleteButtonController: false,
      deleteRoomId: '',
      deleteRoomTableId: '',
      currentPage: 1,
      currentIndex: '',
      pagesize: 10,
      addOrUpdateList: {
        roomTableId: '',
        roomId: '',
        roomNumber: '',
        roomTypeId: '',
        roomTypeName: '',
        roomPrice: '',
        remark: ''
      },
      rooms: {
        roomNumber: [
          {required: true, trigger: 'blur', message: '房间号不能为空'},
          {min: 1, max: 20, message: '请输入房间号', trigger: 'blur'}
        ]
      },
    }
  },
  mounted() {
    this.selectAll()
    this.fetchRoomTypes()  // 加载下拉选项
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage= val
    },
    selectAll() {
      selectAllRoom({
        roomNumber: this.roomNumber,
        roomTypeName: this.roomTypeName,
        roomTypeId: this.roomTypeId
      }).then(
        response => {
          console.log(response.data)
          this.roomList = response.data
          if((this.roomList.slice((this.currentPage-1)*this.pagesize,this.currentPage*this.pagesize)).length == 0){
            this.currentPage = 1
          }
        }
      )
    },
    // 下拉
    fetchRoomTypes() {
      selectAllRoomType().then(response => {
        this.roomOptions = response.data
      })
    },
    addRoom(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertRoom({
            roomNumber: this.addOrUpdateList.roomNumber,
            roomTypeName: this.addOrUpdateList.roomTypeName,
            roomTypeId: this.addOrUpdateList.roomTypeId,
            roomPrice: this.addOrUpdateList.roomPrice,
            remark: this.addOrUpdateList.remark
          }).then(
            response => {
              this.addButtonController = false
              this.$message.success('新增成功！')
              this.selectAll()
            }
          )
        } else {
          return false
        }
      })
    },
    updateRoom(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateRoom({
            roomId: this.addOrUpdateList.roomId,
            roomTableId: this.addOrUpdateList.roomTableId,
            roomNumber: this.addOrUpdateList.roomNumber,
            roomTypeId: this.addOrUpdateList.roomTypeId,
            remark: this.addOrUpdateList.remark
          }).then(
            response => {
              this.updateButtonController = false
              this.$message.success('修改成功！')
              this.selectAll()
            }
          )

        } else {
          return false
        }
      })
    },
    deleteRoom() {
      deleteRoom({
        roomId: this.deleteRoomId,
        roomTableId: this.deleteRoomTableId
      }).then(
        async response => {
          this.deleteButtonController = false
          this.$message.success('删除成功！')
          this.selectAll()
        }
      )
    },
    addButton() {
      if (this.roleName !== '管理员') {
        this.$message.warning('您不是管理员，无法使用本功能');
        return;
      }
      this.addOrUpdateList = {
        roomNumber: '',
        roomTypeId: '',
        // roomTypeName: '',
        // roomPrice: '',
        remark: ''
      }
      this.addButtonController = true
    },
    updateButton(row) {
      if (this.roleName !== '管理员') {
        this.$message.warning('您不是管理员，无法使用本功能');
        return;
      }
      this.addOrUpdateList = {
        roomTableId: row.roomTableId,
        roomId: row.roomId,
        roomTypeId: row.roomTypeId,
        roomNumber: row.roomNumber,
        roomTypeName: row.roomTypeName,
        roomPrice: row.roomPrice,
        remark: row.remark
      }
      this.updateButtonController = true
    },
    deleteButton(row) {
      if (this.roleName !== '管理员') {
        this.$message.warning('您不是管理员，无法使用本功能');
        return;
      }
      this.deleteRoomId = row.roomId
      this.deleteRoomTableId = row.roomTableId
      this.deleteButtonController = true
    },
    checkRoom() {
      this.roomNumber = ''
      this.roomTypeName = ''
      this.roomTypeId = ''
      this.selectAll()
    },
    closeRoom(formName) {
      this.$refs[formName].resetFields()
      this.addButtonController = false
      this.updateButtonController = false
    },
    closeDelete() {
      this.deleteButtonController = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.elegant-room-table {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  overflow: hidden;

  &::before {
    display: none;
  }

  .price-tag {
    display: inline-block;
    padding: 3px 8px;
    color: #171717;
    border-radius: 12px;
    font-weight: 500;
  }
}
</style>
